package com.house.keeper.controller;

import com.alibaba.fastjson.JSONObject;
import com.house.keeper.base.BaseController;
import com.house.keeper.base.Result;
import com.house.keeper.entity.vo.CarVo;
import com.house.keeper.entity.Car;
import com.house.keeper.service.CarService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;
import org.springframework.web.servlet.ModelAndView;
import org.apache.shiro.authz.annotation.RequiresPermissions;

/**
 * @author zjj
 * @date 2022-06-01 08:42:43
 *
 * @description 
 */
@Slf4j
@Controller
@RequestMapping("/car")
public class CarController extends BaseController {

    @Autowired
    private CarService carService;

    /**
      * 跳转到管理页面
      * @return
      */
    @GetMapping(value = "/view")
    @RequiresPermissions("system:car:view")
    public ModelAndView view(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("car/list");
        return modelAndView;
    }
    /**
     * 分页查询
     * @param carVo 查询条件
     * @return
     */
    @GetMapping(value = "/list")
    @RequiresPermissions("system:car:list")
    @ResponseBody
    public JSONObject list(CarVo carVo){
        Result result=carService.selectByPage(carVo);
        return responseJSONObject(result);
    }

    /**
     * 新增或修改
     * @param car
     * @return
     */
    @PostMapping(value = "/update")
    @RequiresPermissions("system:car:edit")
    @ResponseBody
    public JSONObject edit(@RequestBody Car car){
        Result result = carService.update(car);
        return responseJSONObject(result);
    }

    /**
     * 删除
     * @param id id
     * @return
     */
    @GetMapping(value = "/delete")
    @RequiresPermissions("system:car:del")
    @ResponseBody
    public JSONObject edit(Integer id){
        Result result = carService.delete(id);
        return responseJSONObject(result);
    }

    /**
     * 查询小区的所有租用车位的车辆总数
     * @return
     */
    @GetMapping(value = "/getCarNum")
    @ResponseBody
    public JSONObject getCarNum(){
        Result result=carService.getCarNum();
        return responseJSONObject(result);
    }
}

